﻿package KK.net {
	import KK.Queue.IQueueObject;
	import KK.Queue.QObject;
	import KK.System.Pool.IPool;
	import KK.System.Pool.Pool;
	import KK.System.System;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.utils.ByteArray;
	
	public class Q_SwfLoader extends QObject implements IQueueObject , IPool{
		private var loader:Loader;
		private var _url:String;
		private var _params:Object;
		private var _Complete:Function;
		private var _IOError:Function;
		private var _SecurityError:Function;
		private var _Progress:Function;
		private var request:URLRequest;
		private var context:LoaderContext;
		
		private var pool:Pool;
		
		
		protected var type:int;//load==0,loadbytes==1;
		protected var bytes:ByteArray;
		/*
		public function Q_SwfLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null) {
			// constructor code
			init();
			SetLoader(_url,_Complete,_params,_IOError,_SecurityError);
		}
		*/
		
		public function Q_SwfLoader() {
			// constructor code
			init(); 
		}
		
		private function init():void{
			type=0;
			status=0;
			pool=new Pool;
			loader=new Loader();
			request=new URLRequest();
			context=new LoaderContext(true);
			//context.applicationDomain = new ApplicationDomain();    //这个是关键
			context.checkPolicyFile = true;
//			context.securityDomain = SecurityDomain.currentDomain;
			

			//loader.dataFormat=URLLoaderDataFormat.BINARY;
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,Loaded);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,Progress);
			 
		}
		//设置Q_Loader对像
		public function SetLoader(_url:String,_Complete:Function ,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null,_Progress:Function=null):void{
			status=2;
			type=0;
			Reset();
			Using();
			
			this._url=_url;
			this._Complete=_Complete;
			this._params=_params;
			this._IOError=_IOError;
			this._SecurityError=_SecurityError;
			this._Progress=_Progress;
			this.request.data=_params;
			this.request.url=encodeURI(_url);
			loader.name=encodeURI(_url==null?"":_url); 
			 
		}
		
		public function SetBytesLoader(byte:ByteArray,_Complete:Function ,name:String="",_IOError:Function=null,_SecurityError:Function=null,_Progress:Function=null):void
		{
			status=2;
			type=1;
			Reset();
			Using();
			
			
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,Loaded);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,Progress);
			
			loader=new Loader();
			
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,Loaded);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,Progress);
			
			
			
			this._url="";
			this._Complete=_Complete;
			this._params=_params;
			this._IOError=_IOError;
			this._SecurityError=_SecurityError;
			this._Progress=_Progress;
			this.request.data=_params;
			this.request.url=encodeURI(_url);
			loader.name=encodeURI(_url==null?"":name);
			
			this.bytes=byte;
			
		}
		
		
		//用object对象赋值
		public function SetLoaderObject(obj:Object):void{
			SetLoader(obj.url,obj.Complete,obj.params,obj._IOError,obj._SecurityError,obj._Progress);
		}
		//重置对像
		override public function Reset():int{
			//loader.removeEventListener(Event.COMPLETE,Complete);
			//loader.removeEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			//loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error); 
			
			//如果加载重就关闭
			/*if(status==1)
			{
				loader.close();
			}*/
			this._url=null;

			this._Complete=undefined;
			this._params=undefined;
			this._IOError=undefined;
			this._SecurityError=undefined;
			this._Progress=undefined
			UnUsed();
			return super.Reset();
		}
		
		public function Remove():void{
			 loader.removeEventListener(Event.COMPLETE,Complete);
			 loader.removeEventListener(IOErrorEvent.IO_ERROR,IO_Error);
			 loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,Security_Error);
			 loader.unloadAndStop(true);
			 Reset();
			 loader=null;
		}
		/**
		 * 加载完毕 
		 * @param event
		 * 
		 */		
		private function Loaded(event:Event):void{ 
			if(null!=_Complete)
			{
				_Complete(event);
			}
			Complete();
		}
		/**
		 * IO错误，加载失败 
		 * @param event
		 * 
		 */		
		private function IO_Error(event:IOErrorEvent):void{ 
			if(null!=_IOError)
			{
				
				_IOError(event);
			}
			Complete();
		}
		/**
		 * 安全策略拒绝访问，加载失败 
		 * @param event
		 * 
		 */		
		private function Security_Error(event:SecurityErrorEvent):void{ 
			if(null!=_SecurityError)
			{
				_SecurityError(event);
			}
			Complete();
		}
		/**
		 * 加载进度 
		 * @param event
		 * 
		 */		
		private function Progress(event:ProgressEvent):void
		{
			if(null!=_Progress)
			{
				_Progress(event);
			}
		}
		/**
		 * 开始加载 
		 * 
		 */		
		override public function Start():void
		{
			super.Start();
			load(request);
			//loader.load(request);
		}
		/**
		 * 加载 
		 * @param request
		 * 
		 */		
		protected function load(request:URLRequest):void{
			//开始加载资源 
			if(type)
			{
				context.checkPolicyFile=false;
//				context.applicationDomain = null;    //这个是关键
				context.securityDomain = null;
				loader.loadBytes(this.bytes,context);
			}else{
				if(System.Environment==System.NETWORK){
					context.checkPolicyFile=true;
					//context.applicationDomain = new ApplicationDomain();    //这个是关键
					context.securityDomain = SecurityDomain.currentDomain;
					loader.load(request,context);
				}else{
					context.checkPolicyFile=true; 
					context.securityDomain = null;
					loader.load(request,context);
				}
			}
		} 
		
		//////////////////////////////////////////////////////////////// 
		/**
		 * 池状态 
		 * @return 
		 * 
		 */
		public function get PoolStatus():int{
			return pool.PoolStatus;
		}
		/**
		 * 未使用  
		 * @return 
		 * 
		 */
		public function UnUsed():int{
			return pool.UnUsed();
		}
		/**
		 * 使用中  
		 * @return 
		 * 
		 */
		public function Using():int{
			return pool.Using();
		}
	}
	
}
